package controller

import (
	backend "AccessAdmin/api/backend"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

// 菜单管理
var Menu = cMenu{}

type cMenu struct{}

// 渲染菜单列表
func (c *cMenu) ShowIndex(ctx context.Context, req *backend.MenuIndexReq) (res *backend.MenuIndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "菜单列表",
	})
	return
}

// 获取菜单列表
func (c *cMenu) IndexData(ctx context.Context, req *backend.MenuIndexListReq) (res *backend.MenuIndexListRes, err error) {
	res = &backend.MenuIndexListRes{}
	_data, err := service.Menu().GetList(ctx, model.MenuGetListInput{
		Page:       req.Page,
		Size:       req.Limit,
		Title:      req.Title,
		Permission: req.Permission,
		Status:     req.Status,
	})
	if _data != nil && err == nil {
		res.Data = _data.List
		res.Count = _data.Total
	}
	return
}

// 显示菜单创建
func (c *cMenu) ShowCreate(ctx context.Context, req *backend.MenuShowCreateReq) (res *backend.RoleShowCreateRes, err error) {
	menuTree, err := service.Menu().GetMenuTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(menuTree)
	jsonRoute, _ := gjson.EncodeString(g.Server().GetRoutes())
	service.View().Render(ctx, model.View{
		Title:   "菜单创建",
		MainTpl: "admin/menu/form.html",
		Data: g.Map{
			"action": "/admin/menu/create",
			"menuModel": &entity.Menu{
				Status: 1,
				Method: "GET",
				Sort:   100,
			},
			"menuTree": jsonData,
			"route":    jsonRoute,
		},
	})
	return
}

// Create 创建菜单
func (c *cMenu) Create(ctx context.Context, req *backend.MenuCreateReq) (res *backend.MenuCreateRes, err error) {
	out, err := service.Menu().Create(ctx, model.MenuCreateInput{
		MenuCreateUpdateBase: model.MenuCreateUpdateBase{
			Pid:        req.Pid,
			Title:      req.Title,
			Permission: req.Permission,
			Icon:       req.Icon,
			Method:     req.Method,
			Path:       req.Path,
			Type:       req.Type,
			IsHide:     req.IsHide,
			Status:     req.Status,
			Sort:       req.Sort,
		},
	})
	if err != nil {
		return nil, err
	}
	return &backend.MenuCreateRes{MenuId: out.MenuId}, nil
}

// 显示菜单修改
func (c *cMenu) ShowUpdate(ctx context.Context, req *backend.MenuShowUpdateReq) (res *backend.MenuShowUpdateRes, err error) {
	menuModel, err := service.Menu().GetDetail(ctx, model.MenuGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("菜单信息错误!")
	}
	menuTree, err := service.Menu().GetMenuTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(menuTree)
	jsonRoute, _ := gjson.EncodeString(g.Server().GetRoutes())
	service.View().Render(ctx, model.View{
		Title:   "菜单修改",
		MainTpl: "admin/menu/form.html",
		Data: g.Map{
			"action":    "/admin/menu/update/" + gconv.String(menuModel.Id),
			"menuModel": menuModel,
			"menuTree":  jsonData,
			"route":     jsonRoute,
		},
	})
	return
}

// 修改菜单
func (c *cMenu) Update(ctx context.Context, req *backend.MenuUpdateReq) (res *backend.MenuUpdateRes, err error) {
	err = service.Menu().Update(ctx, model.MenuUpdateInput{
		Id: req.Id,
		MenuCreateUpdateBase: model.MenuCreateUpdateBase{
			Pid:        req.Pid,
			Title:      req.Title,
			Permission: req.Permission,
			Icon:       req.Icon,
			Method:     req.Method,
			Path:       req.Path,
			Type:       req.Type,
			IsHide:     req.IsHide,
			Status:     req.Status,
			Sort:       req.Sort,
		},
	})
	return
}
